package com.otp.controller;

import com.otp.pojo.dto.CheckInDto;
import com.otp.pojo.dto.CheckInStatsDto;
import com.otp.pojo.entity.CheckIn;
import com.otp.pojo.request.checkin.CheckInCreateRequest;
import com.otp.service.CheckInService;
import com.otp.service.CourseService;
import com.otp.service.UserService;
import com.otp.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/check-in")
public class CheckInController {

    @Autowired
    private CheckInService checkInService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private UserService userService;

    @PostMapping
    public ResponseMessage<CheckInDto> create(@RequestBody CheckInCreateRequest request) {
        CheckIn checkIn = checkInService.create(request);
        return ResponseMessage.success(checkIn.convertToDto(
                        courseService.getById(checkIn.getCourseId()).getTitle(),
                        userService.getById(checkIn.getStudentId()).getRealName()));
    }

    @GetMapping("/course/{courseId}")
    public ResponseMessage<List<CheckInDto>> listByCourseId(@PathVariable Long courseId) {
        return ResponseMessage.success(checkInService.listByCourseId(courseId).stream()
                .map(checkIn -> {
                    String courseTitle = courseService.getById(checkIn.getCourseId()).getTitle();
                    String studentName = userService.getById(checkIn.getStudentId()).getRealName();
                    return checkIn.convertToDto(courseTitle, studentName);
                })
                .toList());
    }

    @GetMapping("/student/{studentId}")
    public ResponseMessage<List<CheckInDto>> listByStudentId(@PathVariable Long studentId) {
        return ResponseMessage.success(checkInService.listByStudentId(studentId).stream()
                .map(checkIn -> {
                    String courseTitle = courseService.getById(checkIn.getCourseId()).getTitle();
                    String studentName = userService.getById(checkIn.getStudentId()).getRealName();
                    return checkIn.convertToDto(courseTitle, studentName);
                })
                .toList());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<Void> delete(@PathVariable Long id) {
        checkInService.delete(id);
        return ResponseMessage.success(null);
    }

    @GetMapping("/stats/course/{courseId}")
    public ResponseMessage<List<CheckInStatsDto>> getStatsByCourse(@PathVariable Long courseId) {
        return ResponseMessage.success(checkInService.getStatsByCourse(courseId)
                .stream()
                .map(stats -> {
                    String courseTitle = courseService.getById(stats.getCourseId()).getTitle();
                    String studentName = userService.getById(stats.getStudentId()).getRealName();
                    return stats.convertToDto(courseTitle, studentName);
                })
                .toList());
    }

    @GetMapping("/stats/student/{studentId}")
    public ResponseMessage<List<CheckInStatsDto>> getStatsByStudent(@PathVariable Long studentId) {
        return ResponseMessage.success(checkInService.getStatsByStudent(studentId)
                .stream()
                .map(stats -> {
                    String courseTitle = courseService.getById(stats.getCourseId()).getTitle();
                    String studentName = userService.getById(stats.getStudentId()).getRealName();
                    return stats.convertToDto(courseTitle, studentName);
                })
                .toList());
    }
}